import 'package:flutter/material.dart';

import 'shape.dart';

enum DxArcShapePosition { bottom, top, left, right }

enum DxArcShapeDirection { outside, inside }

/// 椭圆
class DxArcShape extends Shape {
  final DxArcShapePosition position;
  final double height;
  final DxArcShapeDirection direction;

  DxArcShape({
    this.position = DxArcShapePosition.bottom,
    this.direction = DxArcShapeDirection.outside,
    this.height = 10,
  });

  @override
  Path build({Rect? rect, double? scale}) {
    return generatePath(rect!, scale);
  }

  Path generatePath(Rect rect, double? scale) {
    final size = rect.size;
    switch (position) {
      case DxArcShapePosition.top:
        if (direction == DxArcShapeDirection.outside) {
          return Path()
            ..moveTo(0.0, height)
            ..quadraticBezierTo(size.width / 4, 0.0, size.width / 2, 0.0)
            ..quadraticBezierTo(size.width * 3 / 4, 0.0, size.width, height)
            ..lineTo(size.width, size.height)
            ..lineTo(0.0, size.height)
            ..close();
        } else {
          return Path()
            ..quadraticBezierTo(size.width / 4, height, size.width / 2, height)
            ..quadraticBezierTo(size.width * 3 / 4, height, size.width, 0.0)
            ..lineTo(size.width, size.height)
            ..lineTo(0.0, size.height)
            ..close();
        }
        break;
      case DxArcShapePosition.bottom:
        if (direction == DxArcShapeDirection.outside) {
          return Path()
            ..lineTo(0.0, size.height - height)
            ..quadraticBezierTo(size.width / 4, size.height, size.width / 2, size.height)
            ..quadraticBezierTo(size.width * 3 / 4, size.height, size.width, size.height - height)
            ..lineTo(size.width, 0.0)
            ..close();
        } else {
          return Path()
            ..moveTo(0.0, size.height)
            ..quadraticBezierTo(size.width / 4, size.height - height, size.width / 2, size.height - height)
            ..quadraticBezierTo(size.width * 3 / 4, size.height - height, size.width, size.height)
            ..lineTo(size.width, 0.0)
            ..lineTo(0.0, 0.0)
            ..close();
        }
        break;
      case DxArcShapePosition.left:
        if (direction == DxArcShapeDirection.outside) {
          return Path()
            ..moveTo(height, 0.0)
            ..quadraticBezierTo(0.0, size.height / 4, 0.0, size.height / 2)
            ..quadraticBezierTo(0.0, size.height * 3 / 4, height, size.height)
            ..lineTo(size.width, size.height)
            ..lineTo(size.width, 0.0)
            ..close();
        } else {
          return Path()
            ..quadraticBezierTo(height, size.height / 4, height, size.height / 2)
            ..quadraticBezierTo(height, size.height * 3 / 4, 0.0, size.height)
            ..lineTo(size.width, size.height)
            ..lineTo(size.width, 0.0)
            ..close();
        }
        break;
      default: //right
        if (direction == DxArcShapeDirection.outside) {
          return Path()
            ..moveTo(size.width - height, 0.0)
            ..quadraticBezierTo(size.width, size.height / 4, size.width, size.height / 2)
            ..quadraticBezierTo(size.width, size.height * 3 / 4, size.width - height, size.height)
            ..lineTo(0.0, size.height)
            ..lineTo(0.0, 0.0)
            ..close();
        } else {
          return Path()
            ..moveTo(size.width, 0.0)
            ..quadraticBezierTo(size.width - height, size.height / 4, size.width - height, size.height / 2)
            ..quadraticBezierTo(size.width - height, size.height * 3 / 4, size.width, size.height)
            ..lineTo(0.0, size.height)
            ..lineTo(0.0, 0.0)
            ..close();
        }
        break;
    }
  }
}
